<?php

class Articles extends DatabaseTable {

    public function __construct($conn) {
        parent::__construct($conn);
    }

    protected function load() {
        $articles_table = "articles_" . $this->getLanguage()->getCode();
        $sections_table = "sections_" . $this->getLanguage()->getCode();
        $images_table = "images_" . $this->getLanguage()->getCode();

        $query = sprintf("SELECT * FROM %s JOIN %s ON %s=%s LEFT JOIN %s ON %s=%s ",
                        mysql_real_escape_string($articles_table),
                        mysql_real_escape_string($sections_table),
                        mysql_real_escape_string($articles_table . ".section_id"),
                        mysql_real_escape_string($sections_table . ".id"),
                        mysql_real_escape_string($images_table),
                        mysql_real_escape_string($articles_table . ".image_id"),
                        mysql_real_escape_string($images_table . ".id")
        );
        $result = mysql_query($query, $this->conn);
        if ($result) {
            while ($row = DatabaseTable::mysql_fetch_alias_array($result)) {
                $article = new Article();
                $id = $row[$articles_table . '.id'];
                $name = $row[$articles_table . '.name'];
                $long_name = $row[$articles_table . '.long_name'];
                $date = $row[$articles_table . '.date'];
                $text = $row[$articles_table . '.text'];
                $section_id = $row[$articles_table . '.section_id'];
                $image_id = $row[$articles_table . '.image_id'];

                $section = new Section();
                $s_id = $row[$sections_table . '.id'];
                $s_name = $row[$sections_table . '.name'];
                $s_long_name = $row[$sections_table . '.long_name'];

                $image = new Image();
                $i_id = $row[$images_table . '.id'];
                $i_name = $row[$images_table . '.name'];
                $i_url = $row[$images_table . '.url'];
                $i_url_mobile = $row[$images_table . '.url_mobile'];

                $section->setId($s_id);
                $section->setName($s_name);
                $section->setLongName($s_long_name);

                $image->setId($i_id);
                $image->setName($i_name);
                $image->setUrl($i_url);
                $image->setUrlMobile($i_url_mobile);

                $article->setId($id);
                $article->setName($name);
                $article->setLongName($long_name);
                $article->setDate($date);
                $article->setText($text);
                $article->setSection($section);
                $article->setImage($image);

                $article = $this->addVideos($article);

                $this->records[$id] = $article;
            }
        }
        mysql_free_result($result);
    }

    private function addVideos(Article $article) {
        $vid = array();
        $text = $article->getText();
        $video_left = "@video#";
        $leftc = strlen($video_left);
        $right = "@";
        $offset = 0;
        do {
            $occ = stripos($text, $video_left, $offset);
            if ($occ) {
                $start = $occ + $leftc;
                $end = stripos($text, $right, $start);
                $id = substr($text, $start, $end - $start);
                $vid[$id] = $this->loadVideo($id);
                $offset = $end;
            }
        } while ($occ);
        $article->setVideos($vid);
        return $article;
    }

    function randomInSection(Section $section) {
        $article = new Article();
        foreach ($this->records as $art) {
            if ($art->getSection()->getId() == $section->getId()) {
                return $art;
            }
        }
        return $article;
    }

    private function loadVideo($id) {
        $videos_table = "video_" . $this->getLanguage()->getCode();
        $query = sprintf("SELECT * FROM %s WHERE id=%s",
                        mysql_real_escape_string($videos_table),
                        mysql_real_escape_string($id));
        $result = mysql_query($query, $this->conn);
        $video = NULL;
        if ($result) {
            $row = mysql_fetch_assoc($result);
            $id = $row['id'];
            $name = $row['name'];
            $url = $row['url'];
            $url_mobile = $row['url_mobile'];
            $code = $row['code'];
            $video = new Video();
            $video->setId($id);
            $video->setUrl($url);
            $video->setUrlMobile($url_mobile);
            $video->setCode($code);
        }
        mysql_free_result($result);
        return $video;
    }

    public function persist(Article $article) {
        $articles_table = "articles_" . $this->getLanguage()->getCode();
        if ($this->get($article->getId()) != NULL) {
            $query = sprintf("UPDATE %s SET %s='%s', %s='%s',%s='%s', %s=%d WHERE %s=%s",
                            mysql_real_escape_string($articles_table),
                            mysql_real_escape_string($articles_table . ".name"),
                            mysql_real_escape_string(utf8_decode($article->getName())),
                            mysql_real_escape_string($articles_table . ".long_name"),
                            mysql_real_escape_string(utf8_decode($article->getLongName())),
                            mysql_real_escape_string($articles_table . ".text"),
                            mysql_real_escape_string(utf8_decode($article->getText())),
                            mysql_real_escape_string($articles_table . ".section_id "),
                            $article->getSection()->getId(),
                            mysql_real_escape_string($articles_table . ".id"),
                            mysql_real_escape_string($article->getId())
            );
            $results = mysql_query($query, $this->conn);
        } else {
            $results = TRUE;
            $available_language = new AvailableLanguages($this->conn);
            foreach ($available_language->getAll() as $id => $language) {
                $articles_table = "articles_" . $language->getCode();
                $query = sprintf("INSERT INTO %s(%s,%s,%s,%s,%s) VALUES('%s', '%s', '%s', '%s', %d)",
                                mysql_real_escape_string($articles_table),
                                mysql_real_escape_string($articles_table . ".name"),
                                mysql_real_escape_string($articles_table . ".long_name"),
                                mysql_real_escape_string($articles_table . ".date"),
                                mysql_real_escape_string($articles_table . ".text"),
                                mysql_real_escape_string($articles_table . ".section_id "),
                                mysql_real_escape_string(utf8_decode($article->getName())),
                                mysql_real_escape_string(utf8_decode($article->getLongName())),
                                mysql_real_escape_string(utf8_decode($article->getDate())),
                                mysql_real_escape_string(utf8_decode($article->getText())),
                                $article->getSection()->getId()
                );
                $results = $results && mysql_query($query, $this->conn);
            }
        }
        return $results;
    }

}

?>
